﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using Mall.Core;
using Mall.Data;
using Solution;
using Webdiyer.WebControls.Mvc;

namespace Mall.AdminSystem.Controllers
{

    /// <summary>
    /// 角色管理
    /// </summary>
    public class RoleController : BaseController
    {
        private RoleCore roleCore = new RoleCore();
        private MenuActionCore maCore = new MenuActionCore();
        // GET: Role
        public ActionResult Index(int id = 0)
        {
            var rs = roleCore.LoadEntities().OrderByDescending(o => o.R_Flag).ToPagedList(id, 10);
            return View(rs);
        }
        /// <summary>
        /// 冻结/解冻
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        [HttpPost]
        public JsonResult Forzen(int id)
        {
            StandardJsonResult rs = new StandardJsonResult();
            var info = roleCore.LoadEntity(o => o.R_Id == id);

            if (info != null)
            {
                var state = info.R_State;
                if (state == 0)
                {
                    state = 1;
                }
                else if (state == 1)
                {
                    state = 0;
                }
                info.R_State = state;
                if (roleCore.Update(info)) //更改状态
                {
                    rs.State = 0;
                }
            }
            return Json(rs, JsonRequestBehavior.AllowGet);
        }

        /// <summary>
        /// 保存编辑
        /// </summary>
        /// <param name="obj"></param>
        /// <returns></returns>
        [HttpPost]
        [ValidateInput(false)]
        public ActionResult Edit(Sys_Role obj)
        {
            obj.R_Time = DateTime.Now;
            if (obj.R_Id == 0) //新增
            {
                roleCore.AddEntity(obj);
            }
            else //修改
            {
                roleCore.Update(obj);
            }

            return RedirectToAction("Index");
        }

        /// <summary>
        /// 查询角色权限
        /// </summary>
        /// <param name="id">角色ID</param>
        /// <returns></returns>
        [HttpPost]
        public JsonResult QueryPermission(int id)
        {
            var info = roleCore.LoadEntity(o => o.R_Id == id);
            var roleMenus = info.R_Permission;
            var menus = maCore.LoadEntities(o => o.A_State == 0).ToList();
            List<dynamic> roleMenusAttr = null;
            bool ckAll = "*".Equals(roleMenus);  //如果是全选，就不要反序列化咯
            if (!string.IsNullOrEmpty(roleMenus) && !ckAll)
            {
                roleMenusAttr = Serializer.FromJson<List<dynamic>>(roleMenus);
            }

            var allMenu = menus.Select(o => new
            {
                id = o.A_Id,
                pId = o.A_PId,
                name = o.A_Title,
                @checked = ckAll || (roleMenusAttr != null && roleMenusAttr.Any(a => a.id == o.A_Id)),
                open = menus.Where(a => a.A_PId == o.A_Id).Any(b => b.A_Flag == 1),//展开菜单，功能点不展开
            }).ToList();

            allMenu.Insert(0, new
            {
                id = 0,
                pId = -1,
                name = "全选",
                @checked = ckAll,
                open = true,

            });
            return Json(allMenu);
        }

        /// <summary>
        /// 保存角色权限
        /// </summary>
        /// <param name="id"></param>
        /// <param name="str"></param>
        /// <returns></returns>
        [HttpPost]
        public JsonResult SavePermission(int id, string str)
        {
            StandardJsonResult rs = new StandardJsonResult();
            if (roleCore.SavePermission(id, str))
            {
                rs.State = 0;
            }
            return Json(rs, JsonRequestBehavior.AllowGet);
        }

    }
}